package fashionshop.bll;

import java.awt.Dialog;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.JOptionPane;

import fashionshop.dal.CustomerDAL;
import fashionshop.dal.OrderDAL;
import fashionshop.dal.ProductDAL;

public class OrderBLL {

	private int id;
	private int customerId;
	private BigDecimal sum;
	private BigDecimal discount;
	private String orderDate;
	private String paymentDate;
	private String note;
	private String status;
	
	public OrderBLL() {
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getCustomerId() {
		return customerId;
	}
	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	}
	public BigDecimal getSum() {
		return sum;
	}
	public void setSum(BigDecimal sum) {
		this.sum = sum;
	}
	public BigDecimal getDiscount() {
		return discount;
	}
	public void setDiscount(BigDecimal discount) {
		this.discount = discount;
	}
	public String getOrderDate() {
		return orderDate;
	}
	public void setOrderDate(String orderDate) {
		this.orderDate = orderDate;
	}
	public String getPaymentDate() {
		return paymentDate;
	}
	public void setPaymentDate(String paymentDate) {
		this.paymentDate = paymentDate;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public static Vector loadAllOrder() throws Exception {
		
		Vector orderTableRecords = new Vector();
		ResultSet result = OrderDAL.LoadAll();
		while (result.next()){
			Vector record = new Vector();
            record.add(result.getInt("ID"));
            record.add(result.getInt("CustomerID"));
            record.add(result.getString("CustomerName"));
            record.add(result.getString("Sum"));
            record.add(result.getString("Discount"));
            record.add(result.getString("OrderDate"));
            record.add(result.getString("PaymentDate"));
            record.add(result.getString("Note"));
            record.add(result.getString("Status"));
            
            orderTableRecords.add(record);
		}
		return orderTableRecords;
	}
	
	public Vector AdvancedSearch(OrderBLL order, String customerName, String orderDateFrom, String orderDateTo, String paymentDateForm, String paymentDateTo) throws Exception {
		
		Vector orderTableRecords = new Vector();
		ResultSet result = OrderDAL.LoadByCriteria(order, customerName, orderDateFrom, orderDateTo, paymentDateForm, paymentDateTo);
		while (result.next()){
			Vector record = new Vector();
			record.add(result.getInt("ID"));
            record.add(result.getInt("CustomerID"));
            record.add(result.getString("CustomerName"));
            record.add(result.getString("Sum"));
            record.add(result.getString("Discount"));
            record.add(result.getString("OrderDate"));
            record.add(result.getString("PaymentDate"));
            record.add(result.getString("Note"));
            record.add(result.getString("Status"));
			
            orderTableRecords.add(record);
		}
		return orderTableRecords;
	}

	public boolean SaveAll(Vector orderTableRecords) throws Exception {
		
		boolean flag = true;
		for(int i = 0 ; i < orderTableRecords.size(); i++){
			Vector vt = (Vector) orderTableRecords.get(i);
			// Only save the order that was updated
			if(vt.get(9) != null){
				if(vt.get(0).toString().isEmpty() || vt.get(0).toString() == null || vt.get(0).toString().equals("")){
					OrderBLL order = new OrderBLL();
					order.setCustomerId(Integer.parseInt(vt.get(1).toString()));
					order.setSum(new BigDecimal(vt.get(3).equals("") ? "0" : vt.get(3).toString()));
					order.setDiscount(new BigDecimal(vt.get(4).equals("") ? "0" : vt.get(4).toString()));
					order.setOrderDate(new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()));
					
					if(vt.get(6) == null || vt.get(6).toString().isEmpty() || vt.get(6).toString().equals("")){
						order.setPaymentDate("");
					}
					else{
						Calendar dateFrom = Calendar.getInstance();
						dateFrom.setTime(Date.valueOf(vt.get(6).toString()));
						order.setPaymentDate(new SimpleDateFormat("yyyy-MM-dd").format(dateFrom.getTime()));
					}
					
					order.setNote(vt.get(7).toString());
					order.setStatus(vt.get(8).toString());
					
					if(OrderDAL.Insert(order) == 0){
						flag = false;
						break;
					}
					else{
						if(order.getStatus() == "Đã thanh toán"){
							CustomerBLL customerBll = new CustomerBLL();
							customerBll.UpdateScore(order.getCustomerId(), order.getSum());
						}
					}
				}
				else{
					OrderBLL order = new OrderBLL();
					order.setId(Integer.parseInt(vt.get(0).toString()));
					order.setCustomerId(Integer.parseInt(vt.get(1).toString()));
					order.setSum(new BigDecimal(vt.get(3).equals("") ? "0" : vt.get(3).toString()));
					order.setDiscount(new BigDecimal(vt.get(4).equals("") ? "0" : vt.get(4).toString()));
					
					Calendar dateFrom = Calendar.getInstance();
					dateFrom.setTime(Date.valueOf(vt.get(5).toString()));
					
					if(vt.get(6) == null || vt.get(6).toString().isEmpty() || vt.get(6).toString().equals("")){
						order.setPaymentDate("");
					}
					else{
						dateFrom.setTime(Date.valueOf(vt.get(6).toString()));
						order.setPaymentDate(new SimpleDateFormat("yyyy-MM-dd").format(dateFrom.getTime()));
					}
						
					order.setNote(vt.get(7).toString());
					order.setStatus(vt.get(8).toString());
					if(OrderDAL.Update(order) == 0){
						flag = false;
						break;
					}
					else{
						if(order.getStatus() == "Đã thanh toán"){
							CustomerBLL customerBll = new CustomerBLL();
							customerBll.UpdateScore(order.getCustomerId(), order.getSum());
						}
					}
				}
			}
		}
			
		return flag;
	}
}
